package com.ym.b2b.tags;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import com.ym.b2b.product.dao.EmBProductTypeDAO;
import com.ym.b2b.product.sqlbean.EmBProductType;
import com.ym.b2b.tags.base.BaseTag;
import com.ym.b2b.type.dao.EmBNewsTypeDAO;
import com.ym.b2b.type.sqlbean.EmBNewsType;
import com.zc.commons.base.Result;
import com.zc.commons.spring.SpringContextHolder;

/**
 * 当前位置 标签
 * 
 * @ClassName: PositionTags
 * @Description: TODO
 * @Author 竺夏军
 * @Date 2015年2月2日 上午11:53:00
 * @Modify
 * @CopyRight 信息化团队
 */
public class PositionTags extends BaseTag {

    private static final long serialVersionUID = 1L;

    protected String          positionType;

    protected Long            typeId           = 0l;
    protected String          typeCode         = "";

    public int doStartTag() throws JspTagException {

        // 判断
        return EVAL_PAGE;
    }

    public int doEndTag() throws JspTagException {
        try {

            if (positionType.equals("cms")) {
                JspWriter out = getPageContext().getOut();
                String ret = doCmsTypePosition();
                // System.out.println("内容分类导航：" + ret);
                out.print(ret);
                out.flush();
            }

            if (positionType.equals("productCategory")) {
                JspWriter out = getPageContext().getOut();
                out.print(doProductTypePosition());
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    /**
     * 获取 内容分类位置
     * 
     * @Title: doCmsTypePosition
     * @Description: TODO
     * @Param @return
     * @Return String
     * @Throws
     */
    private String doCmsTypePosition() {
        Result ret = new Result();
        ret.setResult("");

        // 递归分类
        String old = "<a href='" + getReq().getContextPath() + "'>首页</a>";
        
/*        String old = "<a href='" + getReq().getContextPath() + "'>首页</a>";*/
        if (typeId > 0) {
            EmBNewsTypeDAO emBNewsTypeDAO = SpringContextHolder.getBean("emBNewsTypeDAO");
            recursionNewsType(ret, typeId, emBNewsTypeDAO);
        }
        ret.setResult(old + ret.getResult());
        return ret.getResult();
    }

    private void recursionNewsType(Result ret, Long newsTypeId, EmBNewsTypeDAO emBNewsTypeDAO) {
        if (0l == newsTypeId) {
            return;
        }
        EmBNewsType e = emBNewsTypeDAO.searchEmBNewsType(newsTypeId);
        if (null != e) {
			if (newsTypeId != 278) {
				ret.setResult("&gt;<a href='" + getReq().getContextPath()
						+ "/views/jsp/cms/news/index_d.jsp'>行业资讯</a>&gt;<a href='" + getReq().getContextPath()
						+ "/news/searchPageNews.s?newsTypeId=" + e.getNewsTypeId() + "'>" + e.getName() + "</a>"
						+ ret.getResult());

			} else {
				ret.setResult("&gt;<a href='" + getReq().getContextPath() + "/news/searchPromoProductNews.s?newsTypeId="
						+ e.getNewsTypeId() + "'>" + e.getName() + "</a>" + ret.getResult());
			}
            recursionNewsType(ret, e.getParentid(), emBNewsTypeDAO);
        } else {
            return;
        }
    }

    /**
     * 获取 商品分类 位置
     * 
     * @Title: doProductTypePosition
     * @Description: TODO
     * @Param @return
     * @Return String
     * @Throws
     */
    private String doProductTypePosition() {
        Result ret = new Result();
        ret.setResult("");
        String old = "<li><a href='" + getReq() + "'>首页</a>&gt;</li><li><a href='" + getReq()
                     + "/product/searchProduct.s'>可供商品</a></li>";

        if (typeId > 0) {
            EmBProductTypeDAO emBProductTypeDAO = SpringContextHolder.getBean("emBProductTypeDAO");
            // 递归分类
            recursionProductType(ret, typeId, emBProductTypeDAO);
        }
        ret.setResult(old + ret.getResult());
        return ret.getResult();

    }

    private void recursionProductType(Result ret, Long productTypeId, EmBProductTypeDAO emBProductTypeDAO) {
        if (null == productTypeId || 0l == productTypeId) {
            return;
        }
        EmBProductType e1 = new EmBProductType();
        e1.setProductTypeId(productTypeId);
        EmBProductType e = emBProductTypeDAO.showEmBProductTypeDetail(e1);
        if (null != e) {
            ret.setResult("<li>&gt;<a href='" + getReq().getContextPath() + "/product/searchProduct.s?productTypeId="
                          + e.getProductTypeId() + "'>" + e.getProductTypeName() + "</a></li>" + ret.getResult());

            recursionProductType(ret, e.getParentTypeId(), emBProductTypeDAO);
        } else {
            return;
        }
    }

    public void setPositionType(String positionType) {
        this.positionType = positionType;
    }

    public String getPositionType() {
        return positionType;
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public String getTypeCode() {
        return typeCode;
    }

    public void setTypeCode(String typeCode) {
        this.typeCode = typeCode;
    }

}
